<?php
/**
 * @file WCSearchFactory.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-03-06 02:33:47 +0000 (Wed, 06 Mar 2013) $
 *
 * $Id: WCSearchFactory.php 58 2013-03-06 02:33:47Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/search/WCSearchFactory.php $
 */

require_once dirname(__FILE__)."/WCSearchItems.php";

/**
 * @class WCSearchFactory
 */
class WCSearchFactory {
	public static	$TypePriority = array(
			"CL"	=> 1,	// 4,
			"TA"	=> 1,	// 3,
			"CX"	=> 1,	// 2,
			"DI"	=> 1,	// 1,
			"RL"	=> 1	// 1,
	);
	/**
	 * @var array
	*/
	protected static	$_Separators	= array("-","/",".","[","]","(",")","_","&","+","~","?","°","¿","¡","!","'",'"',":");

	protected function __construct() {
	}
	protected function __destruct() {
	}
	/**
	 * Prevent users to clone the instance.
	 */
	public function __clone() {
		trigger_error(__CLASS__.": Clone is not allowed.", E_USER_ERROR);
	}

	//
	// Public class methods.
	/**
	* @param array $info
	* @param string $hash
	* @return WCSearchItem
	*/
	public static function BuildItem(WCGroup &$group, $info=array(), $hash=false) {
		if(!isset($info["type"])) {
			trigger_error(__CLASS__."::".__FUNCTION__."(): Wrong item information.", E_USER_ERROR);
		}

		$class = "WCSearchItem{$info["type"]}";
		if(!class_exists($class)) {
			trigger_error(__CLASS__."::".__FUNCTION__."(): Class '{$class}' doesn't exist.", E_USER_ERROR);
		}

		return new $class($group, $info, $hash);
	}
	public static function SearchItemHash(&$sitRow) {
		$id = $sitRow["sit_group"].$sitRow["sit_type"];
		switch($sitRow["sit_type"]) {
			case "CL":
				$id.= $sitRow["sit_collection"];
				break;
			case "TA":
				$id.= $sitRow["sit_tag"];
				break;
			case "CX":
				$id.= $sitRow["sit_comic"];
				break;
			case "DI":
				$id.= $sitRow["sit_directory"];
				break;
			case "RL":
				$id.= $sitRow["sit_rule"];
				break;
		}
			
		return $id;
	}
	public static function StringToPatterns($str) {
		$str = strtolower(str_replace(WCSearchFactory::$_Separators, " ", $str));
		$str = array_filter(explode(" ", $str), "WCSearchFactory::WordsFilter");
		$str = array_values($str);

		return $str;
	}
	public static function WordsFilter($value) {
		return $value != "";
	}
}
?>
